<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item label="" prop="name"
      ><el-input v-model="ruleForm.name" placeholder="请输入手机号码"></el-input
    ></el-form-item>
    <el-form-item label="" prop="admitCode" class="inAdminCode"
      ><el-input v-model="ruleForm.admitCode" placeholder="请输入短信验证码">
      </el-input>
      <span class="setAdminCode">发送验证码</span>
    </el-form-item>
    <el-form-item label="" prop="pass"
      ><el-input
        placeholder="请输入密码"
        type="password"
        v-model="ruleForm.pass"
        auto-complete="off"
      ></el-input
    ></el-form-item>
    <el-form-item label="" prop="checkPass"
      ><el-input
        placeholder="请再次输入密码"
        type="password"
        v-model="ruleForm.checkPass"
        auto-complete="off"
      ></el-input
    ></el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "Register",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      activeName: "second",
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
        admitCode: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入您的手机号码", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 11 个字符", trigger: "blur" },
        ],
        admitCode: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 0, max: 6, message: "请输入6位验证码", trigger: "blur" },
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            type: "success",
            message: "注册成功",
          });
          // this.activeName: 'first',
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.inAdminCode {
  width: 170px;
  position: relative;
}
.setAdminCode {
  position: absolute;
  display: inline-block;
  width: 100px;
  height: 40px;
  background-color: var(--color-tint);
  border-color: var(--color-tint);
  color: #f4f4f4;
  border-radius: 4px;
  text-align: center;
  margin-left: 15px;
}
</style>